﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VSDBM.Code.dto;
using System.Data;
using MySql.Data.MySqlClient;

/// <summary>
/// Summary description for Sequence_SubtypeDAO
/// </summary>
/// 
namespace VSDBM.Code.dao
{
    public class Sequence_SubtypeDAO
    {


        public static String add(Sequence_Subtype sub)
        {
            MySqlConnection connection = Connection.getNewConnection();
            try
            {

                
                    MySqlCommand query = new MySqlCommand("INSERT INTO sequence_subtype (idsequence,idsubtype,comparationscore) VALUES ("+sub.idsequence+"," + sub.idsubtype + "," + sub.comparationscore +")", connection);

                    query.ExecuteNonQuery();

                    connection.Close();
                    return "Added!";
                    
            }
            catch (Exception e)
            {
                connection.Close();
                throw;
            }
        }

        public static Boolean delete(Sequence_Subtype sub)
        {
            MySqlConnection connection = Connection.getConnection();
            try
            {
                MySqlCommand query = new MySqlCommand("DELETE FROM sequence_subtype WHERE id = " + sub.id + "", connection);
                query.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                throw;
            }
        }

        public static DataSet select(String filters, String orderby)
        {
            MySqlConnection connection = Connection.getConnection();
            DataSet dSet = new DataSet();
            try
            {

                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = new MySqlCommand("SELECT * FROM sequence_subtype " + filters + " " + orderby, connection);

                adapter.Fill(dSet);
                return dSet;
            }
            catch (Exception e)
            {
                return dSet;
            }
        }

        public static Sequence_Subtype Fill(int id, int idsequence, int idsubtype, float comparationscore)
        {

            Sequence_Subtype seqsub = new Sequence_Subtype(id, idsequence, idsubtype, comparationscore);
            return seqsub;
        }

        public static List<Sequence_Subtype> DStoList(DataSet ds)
        {

            List<Sequence_Subtype> list = new List<Sequence_Subtype>();
            try
            {
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    DataTable registro = ds.Tables[0];
                    list.Add(Fill(int.Parse(registro.Rows[i]["id"].ToString()), int.Parse(registro.Rows[i]["idsequence"].ToString()), int.Parse(registro.Rows[i]["idsubtype"].ToString()), float.Parse(registro.Rows[i]["comparationscore"].ToString())));
                }

            }
            catch (Exception e) { }
            return list;
        }

    





    }
}